<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>护工选择测试页面</title>
  <style>
    * {
      margin: 0;
      padding: 0;
      box-sizing: border-box;
    }
    
    body {
      font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, 'Helvetica Neue', Arial, sans-serif;
      background-color: #f9fafb;
      min-height: 100vh;
      padding: 16px;
    }
    
    @media (min-width: 768px) {
      body {
        padding: 32px;
      }
    }
    
    /* 容器 */
    .container {
      max-width: 896px;
      margin: 0 auto;
    }
    
    /* 标题 */
    .page-title {
      font-size: 24px;
      font-weight: bold;
      color: #1f2937;
      margin-bottom: 24px;
    }
    
    @media (min-width: 768px) {
      .page-title {
        font-size: 30px;
      }
    }
    
    /* 操作按钮区域 */
    .button-section {
      margin-bottom: 24px;
    }
    
    /* 按钮 */
    .open-btn {
      padding: 12px 24px;
      background-color: #3b82f6;
      color: white;
      border: none;
      border-radius: 8px;
      font-weight: 500;
      cursor: pointer;
      transition: background-color 0.2s;
    }
    
    .open-btn:hover {
      background-color: #2563eb;
    }
    
    .open-btn:active {
      transform: scale(0.98);
    }
    
    /* 结果显示区域 */
    .result-box {
      margin-top: 24px;
      padding: 16px;
      background: #f3f4f6;
      border-radius: 8px;
      min-height: 100px;
    }
    
    .result-title {
      font-weight: 600;
      margin-bottom: 8px;
      color: #374151;
    }
    
    .result-content {
      color: #6b7280;
      font-family: 'Courier New', monospace;
      word-break: break-all;
    }
    
    /* 弹窗遮罩层 */
    .modal-overlay {
      position: fixed;
      top: 0;
      left: 0;
      right: 0;
      bottom: 0;
      background-color: rgba(0, 0, 0, 0.5);
      display: none;
      align-items: center;
      justify-content: center;
      z-index: 1000;
    }
    
    .modal-overlay.show {
      display: flex;
    }
    
    /* 弹窗容器 */
    .modal-container {
      position: relative;
      width: 90%;
      max-width: 1200px;
      height: 90vh;
      max-height: 800px;
      background: white;
      border-radius: 8px;
      box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
      overflow: hidden;
      display: flex;
      flex-direction: column;
    }
    
    /* 弹窗头部 */
    .modal-header {
      padding: 16px 20px;
      border-bottom: 1px solid #e5e7eb;
      display: flex;
      justify-content: space-between;
      align-items: center;
      background: #f9fafb;
    }
    
    .modal-title {
      font-size: 18px;
      font-weight: 600;
      color: #111827;
    }
    
    .modal-close {
      background: none;
      border: none;
      font-size: 24px;
      cursor: pointer;
      color: #6b7280;
      padding: 0;
      width: 32px;
      height: 32px;
      display: flex;
      align-items: center;
      justify-content: center;
      border-radius: 4px;
      transition: all 0.2s;
    }
    
    .modal-close:hover {
      background: #e5e7eb;
      color: #111827;
    }
    
    /* iframe 容器 */
    .modal-body {
      flex: 1;
      overflow: hidden;
    }
    
    .modal-iframe {
      width: 100%;
      height: 100%;
      border: none;
    }
    
    /* 响应式 */
    @media (max-width: 768px) {
      .modal-container {
        width: 100%;
        height: 100vh;
        max-height: 100vh;
        border-radius: 0;
      }
      
      .modal-header {
        padding: 12px 16px;
      }
      
      .modal-title {
        font-size: 16px;
      }
    }
  </style>
</head>
<body>
  <div class="container">
    <h1 class="page-title">护工选择测试页面</h1>
    
    <!-- 操作按钮 -->
    <div class="button-section">
      <button id="openBtn" class="open-btn">
        选择护工
      </button>
    </div>
    
    <!-- 结果显示 -->
    <div class="result-box">
      <div class="result-title">选中的护工ID列表：</div>
      <div id="resultContent" class="result-content">
        暂无选择，点击"选择护工"按钮开始选择
      </div>
    </div>
    
    <!-- 弹窗遮罩层 -->
    <div id="modalOverlay" class="modal-overlay">
      <div class="modal-container">
        <div class="modal-header">
          <span class="modal-title">选择护工</span>
          <button id="modalCloseBtn" class="modal-close">×</button>
        </div>
        <div class="modal-body">
          <iframe id="menuIframe" class="modal-iframe" src="menu.html?orgCode=A01A03A01&id=1"></iframe>
        </div>
      </div>
    </div>
  </div>

  <script>
    const openBtn = document.getElementById('openBtn');
    const modalOverlay = document.getElementById('modalOverlay');
    const modalCloseBtn = document.getElementById('modalCloseBtn');
    const menuIframe = document.getElementById('menuIframe');
    const resultContent = document.getElementById('resultContent');

    // 打开弹窗
    openBtn.addEventListener('click', () => {
      modalOverlay.classList.add('show');
      // 重置 iframe（可选：如果需要每次打开都重新加载）
      // menuIframe.src = menuIframe.src;
    });

    // 关闭弹窗
    function closeModal() {
      modalOverlay.classList.remove('show');
    }

    modalCloseBtn.addEventListener('click', closeModal);

    // 点击遮罩层关闭
    modalOverlay.addEventListener('click', (e) => {
      if (e.target === modalOverlay) {
        closeModal();
      }
    });

    // 监听来自 iframe 的消息
    window.addEventListener('message', (event) => {
      // 安全检查：可以验证 event.origin 是否来自预期的域名
      // if (event.origin !== 'https://your-domain.com') return;
      
      const { type, data } = event.data;

      if (type === 'nurse-selector-confirm') {
        // 接收到确定消息
        console.log('收到选中的护工ID列表:', data);
        
        // 更新结果显示
        if (data && data.length > 0) {
          resultContent.textContent = JSON.stringify(data, null, 2);
          resultContent.style.color = '#059669';
        } else {
          resultContent.textContent = '未选择任何护工';
          resultContent.style.color = '#6b7280';
        }
        
        // 关闭弹窗
        closeModal();
      } else if (type === 'nurse-selector-cancel') {
        // 接收到取消消息
        console.log('用户取消了选择');
        
        // 更新结果显示
        resultContent.textContent = '用户取消了选择';
        resultContent.style.color = '#dc2626';
        
        // 关闭弹窗
        closeModal();
      }
    });

    // ESC 键关闭弹窗
    document.addEventListener('keydown', (e) => {
      if (e.key === 'Escape' && modalOverlay.classList.contains('show')) {
        closeModal();
      }
    });
  </script>
</body>
</html>

